﻿define(["jquery"], function ($) {
    var defaults = {
        templateId: null,
        postLink: "",
    }

    var initLoginBox = function (options) {
        var opts = $.extend(defaults, options);

        $(".show-login-form").on("click", function () {
            var offset = $(this).offset();
            var left = offset.left - 330;
            var loginBox = $("#loginBox");
            if (loginBox.length > 0) {
                loginBox.remove();
            }

            var tpl = $(opts.templateId).html();
            loginBox = $(tpl);
            loginBox.attr("id", "loginBox");
            loginBox.css("left", left)
            $("body").append(loginBox);
            loginBox.animate({ top: offset.top + 50, left: left, opacity: 1 }, 100);

            initLoginForm(opts);
        });

        $(document).bind("click", function (event) {
            var e = event || window.event;
            var elem = e.srcElement || e.target;
            while (elem) {
                if (elem.id == "loginBox") {
                    return;
                }
                elem = elem.parentNode;
            }
            var position = $("#loginBox").position();
            if (position) {
                if ((elem == null || elem.id != "loginBox") && position.top > 0) {
                    $("#loginBox").animate({ top: -330, opacity: 0 }, 200);
                }
            }
        });
    };

    var initLoginForm = function (opts) {

        document.getElementById("formHeaderLogin").reset();
        $.validator.unobtrusive.parse("#formHeaderLogin");
        $("#formHeaderLogin").on("submit", function () {
            if (!$("#formHeaderLogin").valid()) {
                return false;
            }
            var username = $("#head_email").val();
            var password = $("#head_pwd").val();
            var rememberme = $("#head_remember").is(":checked") ? 1 : 0;
            $.ajax({
                type: "get",
                //data:{ 'email': email, 'userpwd': userpwd, 'rem': remember },
                async: false,
                url: opts.postLink + '-email-' + username + '-userpwd-' + password + '-remember-' + rememberme,
                dataType: "jsonp",
                jsonp: "callback",//传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名(一般默认为:callback)
                jsonpCallback: "callme",//自定义的jsonp回调函数名称，默认为jQuery自动生成的随机函数名，也可以写"?"，jQuery会自动为你处理数据
                success: function (data) {
                    if (data.success == 1) {
                        var pagen = window.location.pathname;
                        if (pagen.indexOf('/Login') != -1) {
                            window.location.href = document.URL;//"{:U('/@www.'.getDomain())}";
                        }
                        else {
                            window.location.href = window.location.href;
                        }
                    }
                    else {
                        $("#head_notice").show();
                        $("#head_notice").html(data.msg);
                    }
                },
                error: function () {
                    message.alert({ content: "查询失败!" });
                }
            });
            return false;
        });

    }

    return {
        initLoginBox: initLoginBox,
    };
});